class Vehicle < ActiveRecord::Base
  belongs_to :motorcade
  has_many :gps_histories

  def self.query_vehicles(vague_conditions, page)
    conditions = self.generate_query_str vague_conditions
    Vehicle.where(conditions.split(',')).paginate(:page => page, :per_page => Constant::PER_PAGE_20)
  end

  def self.generate_query_str(conditions)
    columns = "1=?"
    values = ",1"
    conditions.each do |column, value|
      value_str =  value.to_s
      if value_str.index '%'
        columns = columns + " and " + column.to_s +  " like ? "
      else
        columns = columns + " and " + column.to_s + "  = ? "
      end
      values = values +","+ value.to_s
    end
    return columns+values
  end
end


#p generate_query_str({:driver_name => '%li%', :name => '%li%', :dd => '9'})